package py.com.fms.utilities;

import java.util.*;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

// Project classes imports

import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.*;
import py.com.fms.fms.persistence.services.*;
import py.com.fms.com.bean.*;
import py.com.fms.com.persistence.services.*;
import py.com.fms.gnd.bean.*;
import py.com.fms.gnd.persistence.services.*;
import py.com.fms.prd.bean.BurdenBean;
import py.com.fms.prd.bean.FamilyBean;
import py.com.fms.prd.bean.GroupBean;
import py.com.fms.prd.bean.SuppliesBean;
import py.com.fms.prd.persistence.services.BurdenServices;
import py.com.fms.prd.persistence.services.FamilyServices;
import py.com.fms.prd.persistence.services.GroupServices;
import py.com.fms.prd.persistence.services.SuppliesServices;
import py.com.fms.session.ServiceSqlMap;
import py.com.fms.session.WithinHolder;
import py.com.fms.ven.bean.CustomerBean;
import py.com.fms.ven.persistence.services.CustomerServices;


@SuppressWarnings("unused")
@Name("convertAssist")
@Scope(ScopeType.APPLICATION)
public class ConvertAssistant {
	
	@Logger
	private Log log;
	@In("#{serviceSqlMap}")
	private ServiceSqlMap sqlMap;
	
	// Convert from boolean to String
	public String booleanToString(boolean check){
		if(check == true){
			return "SI";
		}
		if(check == false){
			return "NO";
		}
		return null;
	}
	
	// Convert from lowerCase to UpperCase
	public String convertField(String data) {
		if (data == null) {
			return null;
		}else{
			return data.trim().toUpperCase();
		}

	}
	
	// Convert from long to String
	public String longToString(Long data) {
		if (data == null) {
			System.out.println("Cannot convert null values");
			return "";
		}else{
			return data.toString().trim();
		}
		
	}
	
	public Long stringToLong(String data) {
		Long value = new Long(0);
		if (data == null) {
			System.out.println("Cannot convert null values");
			return new Long(0);
		}else{
			value = Long.parseLong(data.trim());
			return value;
		}
		
	}
	
	// OBJECTS TO STRING CONVERTERS
	
	
	
	public String getCurrencyCod(Object arg2) {
		if (arg2 instanceof CurrencyBean) {
			CurrencyBean bean = (CurrencyBean) arg2;
			return bean.getCod();

		}
		return null;

	}
	
	public CurrencyBean getCurrencyDesc(Long id) throws CommonException {
		CurrencyServices crSrv = new CurrencyServices(this.sqlMap.getConfig());
		Collection<CurrencyBean> allCurrencies = crSrv.getAllCurrencies();
		for(CurrencyBean bean : allCurrencies){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;
	}
	
	
	
	// Para los rubros de los articulos
	public String getBurdenCod(Object arg2) {
		if (arg2 instanceof BurdenBean) {
			BurdenBean bean = (BurdenBean) arg2;
			return bean.getCod();
	
		}
		return null;
	}
    
	public BurdenBean getBurdenDesc(Long id) throws CommonException {
		BurdenServices crSrv = new BurdenServices(this.sqlMap.getConfig());
		Collection<BurdenBean> allBurden = crSrv.getAllBurden();
		for(BurdenBean bean : allBurden){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;
	}
	
	
	
	
	// Para los grupos de los articulos
	public String getGroupCod(Object arg2) {
		if (arg2 instanceof GroupBean) {
			GroupBean bean = (GroupBean) arg2;
			return bean.getCod();
	
		}
		return null;
	}
    
	public GroupBean getGroupDesc(Long id) throws CommonException {
		GroupServices crSrv = new GroupServices(this.sqlMap.getConfig());
		Collection<GroupBean> allGroup = crSrv.getAllGroup();
		for(GroupBean bean : allGroup){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;
	}
	
	
	// Para las familias de los articulos
	public String getFamilyCod(Object arg2) {
		if (arg2 instanceof FamilyBean) {
			FamilyBean bean = (FamilyBean) arg2;
			return bean.getCod();
	
		}
		return null;
	}
    
	public FamilyBean getFamilyDesc(Long id) throws CommonException {
		FamilyServices crSrv = new FamilyServices(this.sqlMap.getConfig());
		Collection<FamilyBean> allFamily = crSrv.getAllFamily();
		for(FamilyBean bean : allFamily){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;
	}
	
	
	public String getCountryCod(Object arg2) {
		if (arg2 instanceof CountryBean) {

			CountryBean bean = (CountryBean) arg2;
			return bean.getCod();

		} else {

			System.out.println("Is not a instance of Country");
			return null;
		}

	}
	
	public String getCountryDesc(Object arg2) {
		if (arg2 instanceof CountryBean) {

			CountryBean bean = (CountryBean) arg2;
			return bean.getDescrip();

		} else {

			System.out.println("Is not a instance of Country");
			return null;
		}

	}
	
	public Long getPersonId(Object arg2) {
		if (arg2 instanceof PersonBean) {
			PersonBean bean = (PersonBean) arg2;
			return bean.getId();

		}
		return null;

	}
	
	public PersonBean getPersonData(Object arg2) {
		if (arg2 instanceof PersonBean) {
			PersonBean bean = (PersonBean) arg2;
			return bean;

		}
		return null;

	}
	
	
	
	public PersonBean getPersonCod(Long id) throws CommonException {
		PersonServices prSrv = new PersonServices(this.sqlMap.getConfig());
		Collection<PersonBean> allPersons = prSrv.getAllPersons();
		for(PersonBean bean : allPersons){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;

	}
	
	
	
	public String getEmpCod(Long id) throws CommonException {
		EmployeeServices emSrv = new EmployeeServices(this.sqlMap.getConfig());
		Collection<EmployeeBean> allEmpl = emSrv.getAllEmployees();
		for(EmployeeBean bean : allEmpl){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean.getCod();
			}
		}
		return null;

	}
	
	public Long getEntId(Object arg2) {
		if (arg2 instanceof EnterpriseBean) {
			EnterpriseBean bean = (EnterpriseBean) arg2;
			return bean.getId();

		}
		return null;

	}
	
    // por que este metodo tiene el nombre como si 
    // fuera a devolver el codigo de empresa cuando 
    // esta devolviendo el bean
	public EnterpriseBean getEntCod(Object arg2) {
		if (arg2 instanceof EnterpriseBean) {
			EnterpriseBean bean = (EnterpriseBean) arg2;
			return bean;

		}
		return null;

	}
	
	public EnterpriseBean getEntCod(Long id) throws CommonException {
		EnterpriseServices enSrv = new EnterpriseServices(this.sqlMap.getConfig());
		Collection<EnterpriseBean> allEnterprises = enSrv.getAllEnterprises();
		for(EnterpriseBean bean : allEnterprises){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;

	}
	
	
	public String getEntName(Long idPerson) throws CommonException{
		PersonServices perSrv = new PersonServices(this.sqlMap.getConfig());
		return perSrv.getDescPerson(idPerson);
	}
	
	
	public RefHeadBean getRefHeadData(Object arg2) {
		if (arg2 instanceof RefHeadBean) {
			RefHeadBean bean = (RefHeadBean) arg2;
			return bean;

		}
		return null;

	}
	
	public Long getRefDetId(Object arg2) {
		if (arg2 instanceof RefDetBean) {
			RefDetBean bean = (RefDetBean) arg2;
			return bean.getId();

		}
		return null;

	}
	
	public RefDetBean getRefDetDesc(Long id) throws CommonException {
		RefServices rnSrv = new RefServices(this.sqlMap.getConfig());
		Collection<RefDetBean> allRefDet = rnSrv.getAllRefDet();
		for(RefDetBean bean : allRefDet){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean;
			}
		}
		return null;

	}

	
	public String getRefDetDescrip(Long id) throws CommonException {
		RefServices rnSrv = new RefServices(this.sqlMap.getConfig());
		Collection<RefDetBean> allRefDet = rnSrv.getAllRefDet();
		for(RefDetBean bean : allRefDet){
			if(bean.getId().equals(id)){
				System.out.println(bean.getId() + " = " + id);
				return bean.getDescrip();
			}
		}
		return null;

	}
	
	
	public String getCustomerCod(Long id) throws CommonException {
		CustomerServices clSrv = new CustomerServices(this.sqlMap.getConfig());
		Collection<CustomerBean> allCustomer = clSrv.getAllCustomers();
		for(CustomerBean bean : allCustomer){
			if(bean.getId() == id){
				System.out.println(bean.getId() + " = " + id);
				return bean.getCod();
			}
		}
		return null;

	}
	
	public String getCustomerName(Long id) throws CommonException {
		Long idPer;
		CustomerServices emSrv = new CustomerServices(this.sqlMap.getConfig());
		Collection<CustomerBean> allCustomer = emSrv.getAllCustomers();
		for(CustomerBean client : allCustomer){
			if(client.getId() == id){
				System.out.println("Customer: " + client.getId() + " = " + id);
				idPer = client.getIdPerson();
				PersonServices prSrv = new PersonServices(this.sqlMap.getConfig());
				Collection<PersonBean> allPersons = prSrv.getAllPersons();
				for(PersonBean person : allPersons){
					if(person.getId() == idPer){
						System.out.println("Person: " + person.getId() + " = " + idPer);
						return person.getName();
					}
				}
			}
		}
		return null;

	}
	
	
	// Convert Methods for GND Module
	
	public String getSympString(Object arg2) {
	    if (arg2 instanceof SymptomBean) {
	     SymptomBean bean = (SymptomBean) arg2;
	     return bean.getCod();
	     
	    }
	    return null;
	    
	  }
	
	public String getSympDesc(Object arg2) {
	    if (arg2 instanceof SymptomBean) {
	     SymptomBean bean = (SymptomBean) arg2;
	     return bean.getDescrip();
	    }
	    return null;
	    
	  }
	
	
	public String getAfflicString(Object arg2) {
	    if (arg2 instanceof AfflictionBean) {
	    	AfflictionBean bean = (AfflictionBean) arg2;
	     return bean.getCod();
	     
	    }
	    return null;
	    
	  }
	
	
	
	
	// FILTERED QUERIES
	
	
	public String getCountryDescById(Long id) throws CommonException {
		CountryServices cnSrv = new CountryServices(this.sqlMap.getConfig());
		String desc = cnSrv.getDescCounrty(id);
		return desc;

	}
	
	public String getCurrencyDescById(Long id) throws CommonException {
		CurrencyServices crSrv = new CurrencyServices(this.sqlMap.getConfig());
		String desc = crSrv.getDescCurrency(id);
		return desc;

	}
	
	
	public String getBurdenDescByid(Long id) throws CommonException {
		BurdenServices brSrv = new BurdenServices(this.sqlMap.getConfig());
		String desc = brSrv.getDescBurden(id);
		return desc;

	}
	
	public String getPersonDescById(Long id) throws CommonException {
		PersonServices prSrv = new PersonServices(this.sqlMap.getConfig());
		String desc = prSrv.getDescPerson(id);
		return desc;

	}
	
	public BurdenBean getBurdenData(Long id) {	
		try {
			BurdenServices admSrv = new BurdenServices(this.sqlMap.getConfig());
			BurdenBean bean = admSrv.getBurdenData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	public GroupBean getGroupData(Long id) {	
		try {
			GroupServices admSrv = new GroupServices(this.sqlMap.getConfig());
			GroupBean bean = admSrv.getGroupData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	public FamilyBean getFamilyData(Long id) {	
		try {
			FamilyServices admSrv = new FamilyServices(this.sqlMap.getConfig());
			FamilyBean bean = admSrv.getFamilyData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	/*
	 *  PERSON ------------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	public String getPersonName(Long idPer) throws CommonException {
		try {
			PersonServices admSrv = new PersonServices(this.sqlMap.getConfig());
			PersonBean bean = admSrv.getPersonData(idPer);
			return bean.getName();
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	
	/*
	 *  SUPPLIERS ------------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public SupplierBean getSupplierData(Long id){
		try {
			SupplierServices admSrv = new SupplierServices(this.sqlMap.getConfig());
			SupplierBean bean = admSrv.getSupplierData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String getSupplierName(Long id){
		try {
			SupplierBean bean = this.getSupplierData(id);
			PersonServices admSrv = new PersonServices(this.sqlMap.getConfig());
			String name = admSrv.getDescPerson(bean.getIdPerson());
			return name;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/*
	 *  BUY CONDITION --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public String getBuyConditionDesc(Long id){
		try {
			BuyConditionServices admSrv = new BuyConditionServices(this.sqlMap.getConfig());
			String name = admSrv.getBuyConditionDesc(id);
			return name;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public BuyConditionBean getBuyConditionData(Long id){
		try {
			BuyConditionServices admSrv = new BuyConditionServices(this.sqlMap.getConfig());
			BuyConditionBean bean = admSrv.getBuyConditionById(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/*
	 * REFERENCE --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public RefDetBean getRefDetData(Long id){
		try {
			RefServices admSrv = new RefServices(this.sqlMap.getConfig());
			RefDetBean bean = admSrv.getRefDetBeanById(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 *  CURRENCY --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public CurrencyBean getCurrencyData(Long id){
		try {
			CurrencyServices admSrv = new CurrencyServices(this.sqlMap.getConfig());
			CurrencyBean bean = admSrv.getCurrencyData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 *  SUPPLIES --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public SuppliesBean getSupplyData(Long id){
		try {
			SuppliesServices admSrv = new SuppliesServices(this.sqlMap.getConfig());
			SuppliesBean bean = admSrv.getSuppliesData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String getSupplyDesc(Long id){
		try {
			SuppliesServices admSrv = new SuppliesServices(this.sqlMap.getConfig());
			SuppliesBean bean = admSrv.getSuppliesData(id);
			return bean.getDescrip();
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 *  MEASURE UNITS --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public MeasureUnitBean getMeasureUnitData(Long id){
		try {
			MeasureUnitServices admSrv = new MeasureUnitServices(this.sqlMap.getConfig());
			MeasureUnitBean bean = admSrv.getMeasureUnitData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 *  TAXES --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public IvaBean getTaxData(Long id){
		try {
			IvaServices admSrv = new IvaServices(this.sqlMap.getConfig());
			IvaBean bean = admSrv.getTaxData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String getTaxDesc(Long id){
		try {
			IvaServices admSrv = new IvaServices(this.sqlMap.getConfig());
			IvaBean bean = admSrv.getTaxData(id);
			return bean.getDescrip();
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 *  VOUCHER --------------------------------------------------------
	 *  
	 *  
	 *  ______________________________________________________________________
	 */
	
	public VoucherBean getVoucherData(Long id){
		try {
			VoucherServices admSrv = new VoucherServices(this.sqlMap.getConfig());
			VoucherBean bean = admSrv.getVoucherData(id);
			return bean;
		} catch (CommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	
//	public String getServiceDescById(Long id) throws CommonException {
//		ServServices prSrv = new ServServices(this.sqlMap.getConfig());
//		String desc = prSrv.getDescService(id);
//		return desc;
//
//	}
	
	
//	public ServPercentBean getServPercData(Long idServ, Long percKindId, String percKindDesc) throws CommonException {
//		System.out.println(idServ);
//		System.out.println(percKindId);
//		System.out.println(percKindDesc);
//		ServPercentBean servPerc = new ServPercentBean();
//		ServPercentBean bean = new ServPercentBean();
//		servPerc.setIdServ(idServ);
//		servPerc.setServPercKind(percKindId);
//		servPerc.setServPercKindDesc(percKindDesc);
//		
//		ServPercentServices spSrv = new ServPercentServices(this.sqlMap.getConfig());
//		bean = spSrv.getServPercentData(servPerc);
//		System.out.println("ServPerc in Convert: " + bean);
//		return bean;
//
//	}
	
//	public ServicesBean getServiceData(Long idServ) {	
//		ServServices srSrv;
//		try {
//			srSrv = new ServServices(this.sqlMap.getConfig());
//			ServicesBean service = srSrv.getServiceById(idServ);
//			return service;
//		} catch (CommonException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//
//	}
	
//	public DependDetBean getDependDetData(Long idDependDet) {	
//		DependServices dpSrv;
//		try {
//			dpSrv = new DependServices(this.sqlMap.getConfig());
//			DependDetBean dependDet = dpSrv.getDependDetById(idDependDet);
//			return dependDet;
//		} catch (CommonException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return null;
//
//	}
	

//	public Long getRefHeadId(String param) throws CommonException {
//		Long refHeadId = new Long(0);
//		
//		RefServices rnSrv = new RefServices(this.sqlMap.getConfig());
//		refHeadId = rnSrv.getIdHeadByDesc(param.toUpperCase());
//		return refHeadId;
//
//	}

	//GETTERS AND SETTERS
	
	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

	public ServiceSqlMap getSqlMap() {
		return sqlMap;
	}

	public void setSqlMap(ServiceSqlMap sqlMap) {
		this.sqlMap = sqlMap;
	}

}
